<?php
/**
 * 功能描述：	前端公共部分
 * @datetime	2022-08-08
 * @version   	v2.3.0
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Web_BaseServe {

    ///////////////////////////////////////【通用】////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////

    /**
     * Notes:通用数据表_添加
     * User: ZhuHaili
     */
    static function addData($table, $data, $returnId = 'id'){
        if(!$table){ return false; }
        if(!$data){ return false; }
        return Model::conn('mysql')->addData($table, $data, $returnId);
    }

    /**
     * Notes:通用数据表_修改
     * User: ZhuHaili
     */
    static function updateData($table, $data, $where=array(), $line = false){
        if(!$table){ return false; }
        if(!$data){ return false; }
        $sql['data'] = $data;
        $sql['table'] = $table;
        if($where){
            $sql['where'] = $where;
        }
        $db = Model::conn('mysql');
        $result = $db->update(self::sqlBind($sql));
        if($line){
            return $db->rowCount() > 0 ? $db->rowCount() : 0;
        }
        return $result;
    }

    /**
     * Notes:通用数据表_删除
     * User: ZhuHaili
     */
    static function delData($table, $where=array(), $line = false){
        if(!$table){ return false; }
        $sql['table'] = $table;
        if($where){
            $sql['where'] = $where;
        }
        $db = Model::conn('mysql');
        $result = $db->del(self::sqlBind($sql));
        if($db){
            return $db->rowCount() > 0 ? $db->rowCount() : 0;
        }
        return $result;
    }

    /**
     * Notes:获取表数据
     * User: ZhuHaili
     */
    static function findData($table, $where=array(), $field = '*', $orderby = ''){
        if(!$table){ return array(); }
        $sql['field'] = $field ? $field : '*';
        $sql['table'] = $table;
        if($where){
            $sql['where'] = $where;
        }
        if($orderby){
            $sql['order'] = $orderby;
        }
        return Model::conn('mysql')->find(self::sqlBind($sql));
    }

    /**
     * Notes:获取列表数据
     * User: ZhuHaili
     */
    static function listData($table, $where=array(), $field = '*', $orderby = '', $limit = ''){
        if(!$table){ return array(); }
        $sql['field'] = $field ? $field : '*';
        $sql['table'] = $table;
        if($where){
            $sql['where'] = $where;
        }
        if($limit){
            $sql['limit'] = $limit;
        }
        if($orderby){
            $sql['order'] = $orderby;
        }
        return Model::conn('mysql')->select(self::sqlBind($sql));
    }

    /**
     * Notes: sql where处理
     * User: ZhuHaili
     * Date: 2022/11/25
     */
    static function sqlBind($sql){
        if(isset($sql['where']['locate'])){
            if(!empty($sql['where']['locate'])){
                $sql['locate'] = $sql['where']['locate'];
            }
            unset($sql['where']['locate']);
        }
        if(isset($sql['where']['in'])){
            if(!empty($sql['where']['in'])){
                $sql['in'] = $sql['where']['in'];
            }
            unset($sql['where']['in']);
        }
        if(isset($sql['where']['gt'])){
            if(!empty($sql['where']['gt'])){
                $sql['gt'] = $sql['where']['gt'];
            }
            unset($sql['where']['gt']);
        }
        if(isset($sql['where']['gts'])){
            if(!empty($sql['where']['gts'])){
                $sql['gts'] = $sql['where']['gts'];
            }
            unset($sql['where']['gts']);
        }
        if(isset($sql['where']['lt'])){
            if(!empty($sql['where']['lt'])){
                $sql['lt'] = $sql['where']['lt'];
            }
            unset($sql['where']['lt']);
        }
        if(isset($sql['where']['lts'])){
            if(!empty($sql['where']['lts'])){
                $sql['lts'] = $sql['where']['lts'];
            }
            unset($sql['where']['lts']);
        }
        return $sql;
    }

    ///////////////////////////////////////【菜单】////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////


    /**
     * Notes: 菜单路由设置
     * User: ZhuHaili
     * Date: 2020/11/18
     */
    static function navseturl($list){
        foreach($list as $k=>$info){
            $list[$k]['url'] = getMenuUrl($info['id'], $info['tmpl'], $info['tmpltype'], $info['gourl'], $info['weburl']);
        }
        return $list;
    }

    /**
     * Notes:菜单设置-无限菜单树
     * User: ZHL
     * Date: 2020/9/29
     */
    static function navset($list){
        if(empty($list)){
            return array();
        }
        $list = self::navseturl($list);
        return getTree($list);
    }

    /**
     * Notes:获取全部菜单
     * User: ZHL
     * Date: 2020/9/29
     */
    static function menuList($where = array()){
        if(!$where){
            // 默认状态开启且后台左侧菜单
            $where['status'] = '1';
            $where['navtype'] = '2'; // 左侧导航
            $where['homeflag'] = '1'; // 前端导航开关开启
        }else{
            $where['status'] = isset($where['status']) ? $where['status'] : '1';
            $where['navtype'] = isset($where['navtype']) ? $where['navtype'] : '2'; // 左侧导航
            $where['homeflag'] = isset($where['homeflag']) ? $where['homeflag'] : '1'; // 前端导航开关开启
        }
        $list = Model::conn('mysql')->listData('sysmenu', $where, '*', 'viewseq desc,id asc,createdate asc');
        return self::navset($list);
    }

    /**
     * Notes:获取子菜单列表
     * User: ZHL
     * Date: 2020/9/29
     */
    static function menuFinds($fid, $status=0){
        $fid = is_numeric($fid) ? intval($fid) : 0;
        $status = is_numeric($status) ? intval($status) : 0;
        $where['fid'] = $fid;
        if($status){
            $where['status'] = $status;
        }
        $list = Model::conn('mysql')->listData('sysmenu', $where, '*', 'viewseq desc,id asc,createdate asc');
        return self::navset($list);
    }

    /**
     * Notes:获取面包屑菜单
     * User: ZHL
     * Date: 2020/11/18
     */
    static function menuCrumb($id, $status=0){
        $where = array();
        if($status){
            $where['status'] = is_numeric($status) ? intval($status) : 0;
        }
        $list = Model::conn('mysql')->listData('sysmenu', $where, '*', 'viewseq desc,id asc,createdate asc');
        $arr = self::navseturl($list);
        return menuCrumbs($arr, $id);
    }

    /**
     * Notes:菜单详情
     * User: ZHL
     * Date: 2020/9/29
     */
    static function menuInfo($id){
        $info = Model::conn('mysql')->findData('sysmenu', array('id'=>$id));
        if(!empty($info)){
            $info['url'] = getMenuUrl($info['id'], $info['tmpl'], $info['tmpltype'], $info['gourl'], $info['weburl']);
        }
        return $info;
    }

    /**
     * Notes:菜单详情
     * User: ZHL
     * Date: 2020/9/29
     */
    static function menuInfos($where){
        if(empty($where)){
            return array();
        }
        $info = Model::conn('mysql')->findData('sysmenu', $where);
        if(!empty($info)){
            $info['url'] = getMenuUrl($info['id'], $info['tmpl'], $info['tmpltype'], $info['gourl'], $info['gourl']);
        }
        return $info;
    }

    ///////////////////////////////////////【文章】////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////


    /**
     * Notes:散文详情-单表
     * User: ZHL
     * Date: 2023/03/17
     */
    static function essayInfo($table, $eid=0, $where=array()){
        $table = is_string($table) ? $table : '';
        $eid = is_numeric($eid) ? intval($eid) : 0;
        if(!$table){return array();}
        $w = array();
        if($eid){
            $w['a.id'] = $eid; // 根据排序获取第一条有时候不用where
        }
        if($where){
            $w = array_merge($w, $where);
        }
        if($w){
            $sql['where'] = $w;
        }
        $sql['field'] = 'a.*,a.id eid,m.tmplinfo,m.menuname,m.mould_code';
        $sql['table'] = $table;
        $sql['as'] = 'a';
        $sql['leftjoin']['sysmenu'] = 'm on a.menu_id=m.id';
        $info = Model::conn('mysql')->find(self::sqlBind($sql));
        if($info){
            $info['url'] = getEssayUrl($info['id'], $info['tmplinfo'], $info['mould_code']);
        }
        return $info;
    }

    /**
     * Notes:散文详情-主附表
     * User: ZHL
     * Date: 2023/03/17
     */
    static function essayInfos($table, $eid=0, $where=array()){
        $table = is_string($table) ? $table : '';
        $eid = is_numeric($eid) ? intval($eid) : 0;
        if(!$table){return array();}
        $w = array();
        if($eid){
            $w['a.id'] = $eid; // 根据排序获取第一条有时候不用where
        }
        if($where){
            $w = array_merge($w, $where);
        }
        if($w){
            $sql['where'] = $w;
        }
        $sql['field'] = 'a.*,a.id eid,b.*,m.tmplinfo,m.menuname,m.mould_code';
        $sql['table'] = $table;
        $sql['as'] = 'a';
        $sql['leftjoin'][$table.'_detail'] = 'b on a.id=b.essay_id';
        $sql['leftjoin']['sysmenu'] = 'm on a.menu_id=m.id';
        $info = Model::conn('mysql')->find(self::sqlBind($sql));
        if($info){
            $info['url'] = getEssayUrl($info['id'], $info['tmplinfo'], $info['mould_code']);
        }
        return $info;
    }

    /**
     * Notes: 获取散文上一条记录
     * User: ZhuHaili
     * Date: 2023/03/17
     * Desc:$menuId=0不限栏目，$status=0不限状态1开启，2关闭
     */
    static function prevInfo($table, $menuId=0, $eid, $status=1){
        $eid = is_numeric($eid) ? intval($eid) : 0;
        $table = is_string($table) ? $table : '';
        $menuId = is_numeric($menuId) ? $menuId : 0;
        if(!$table){return array();}
        if(!$eid){return array();}
        $commM = new Web_BaseModel();
        $info = $commM->prevNextInfo($table, $menuId, $eid, $status, '<');
        if(!empty($info)){
            $info['url'] = getEssayUrl($info['id'], $info['tmplinfo'], $info['mould_code']);
        }
        return $info;
    }

    /**
     * Notes: 获取散文下一条记录
     * User: ZhuHaili
     * Date: 2023/03/17
     * Desc:$menuId=0不限栏目，$status=0不限状态1开启，2关闭
     */
    static function nextInfo($table, $menuId=0, $eid, $status=1){
        $eid = is_numeric($eid) ? intval($eid) : 0;
        $table = is_string($table) ? $table : '';
        $menuId = is_numeric($menuId) ? $menuId : 0;
        if(!$table){return array();}
        if(!$eid){return array();}
        $commM = new Web_BaseModel();
        $info = $commM->prevNextInfo($table, $menuId, $eid, $status, '>');
        if(!empty($info)){
            $info['url'] = getEssayUrl($info['id'], $info['tmplinfo'], $info['mould_code']);
        }
        return $info;
    }

    /**
     * Notes:文章列表/散文列表-单表
     * User: ZHL
     * Date: 2023/03/17
     * Desc: $menuId=0不限栏目，where可以覆盖menuId，where['a.status']=0可以不限状态，默认1
     */
    static function essayList($table, $menuId=0, $limit = 0, $where=array(), $orderby = '', $field = '', $group = ''){
        $table = is_string($table) ? $table : '';
        $menuId = is_numeric($menuId) ? $menuId : 0;
        $limit = is_numeric($limit) ? $limit : 0;
        if(!$table){ return array(); }
        if($menuId){
            $where['a.menu_id'] = isset($where['a.menu_id']) ? $where['a.menu_id'] : $menuId;
        }
        if(isset($where['a.status'])){
            if($where['a.status']){
                $where['a.status'] = $where['a.status'];
            }
        }else{
            $where['a.status'] = 1;
        }
        $sql['field'] = 'a.*,a.id eid,m.tmplinfo,m.mould_code';
        $sql['field'] .= $field ? ','.$field : '';
        $sql['table'] = $table;
        $sql['as'] = 'a';
        $sql['leftjoin']['sysmenu'] = 'm on a.menu_id=m.id';
        if($where){
            $sql['where'] = $where;
        }

        $sql['order'] = $orderby ? $orderby : 'a.viewseq desc,a.createdate desc,a.id desc';

        if($group){
            $sql['group'] = $group;
        }
        if($limit){
            $sql['limit'] = $limit;
        }
        $list = Model::conn('mysql')->select(self::sqlBind($sql));
        if(empty($list)){
            return array();
        }
        foreach($list as $k=>$v){
            $list[$k]['url'] = getEssayUrl($v['id'], $v['tmplinfo'], $v['mould_code']);
        }
        return $list;
    }

    /**
     * Notes:文章列表/散文列表-主附表
     * User: ZHL
     * Date: 2023/03/17
     * Desc: $menuId=0不限栏目，where可以覆盖menuId，where['a.status']=0可以不限状态，默认1
     */
    static function essayLists($table, $menuId=0, $limit = 0, $where=array(), $orderby = '', $field = '', $group = ''){
        $table = is_string($table) ? $table : '';
        $menuId = is_numeric($menuId) ? $menuId : 0;
        $limit = is_numeric($limit) ? $limit : 0;
        if(!$table){ return array(); }
        if($menuId){
            $where['a.menu_id'] = isset($where['a.menu_id']) ? $where['a.menu_id'] : $menuId;
        }
        if(isset($where['a.status'])){
            if($where['a.status']){
                $where['a.status'] = $where['a.status'];
            }
        }else{
            $where['a.status'] = 1;
        }
        $sql['field'] = 'a.*,a.id eid,b.*,m.tmplinfo,m.mould_code';
        $sql['field'] .= $field ? ','.$field : '';
        $sql['table'] = $table;
        $sql['as'] = 'a';
        $sql['leftjoin'][$table.'_detail'] = 'b on a.id=b.essay_id';
        $sql['leftjoin']['sysmenu'] = 'm on a.menu_id=m.id';
        if($where){
            $sql['where'] = $where;
        }
        $sql['order'] = $orderby ? $orderby : 'a.viewseq desc,a.createdate desc,a.id desc';
        if($group){
            $sql['group'] = $group;
        }
        if($limit){
            $sql['limit'] = $limit;
        }
        $list = Model::conn('mysql')->select(self::sqlBind($sql));
        if(empty($list)){
            return array();
        }
        foreach($list as $k=>$v){
            $list[$k]['url'] = getEssayUrl($v['id'], $v['tmplinfo'], $v['mould_code']);
        }
        return $list;
    }

    /**
     * Notes:文章列表/散文列表-多栏目
     * User: ZHL
     * Date: 2023/03/17
     * Desc: $menuId=0不限栏目，where可以覆盖menuId，where['a.status']=0可以不限状态，默认1
     */
    static function essayListMore($table, $menuId=0, $limit = 0, $where=array(), $orderby = '', $field = '', $group = ''){
        $table = is_string($table) ? $table : '';
        $menuId = is_numeric($menuId) ? $menuId : 0;
        $limit = is_numeric($limit) ? $limit : 0;
        if(!$table){ return array(); }
        if($menuId){
            $where['c.menu_id'] = isset($where['c.menu_id']) ? $where['c.menu_id'] : $menuId;
        }
        if(isset($where['a.status'])){
            if($where['a.status']){
                $where['a.status'] = $where['a.status'];
            }
        }else{
            $where['a.status'] = 1;
        }
        $sql['field'] = 'a.*,a.id eid,b.*,m.tmplinfo,m.mould_code';
        $sql['field'] .= $field ? ','.$field : '';
        $sql['table'] = $table;
        $sql['as'] = 'a';
        $sql['leftjoin'][$table.'_detail'] = 'b on a.id=b.essay_id';
        $sql['leftjoin']['sysmenu'] = 'm on a.menu_id=m.id';
        $sql['leftjoin'][$table.'_class'] = ' c on a.id=c.essay_id';
        if($where){
            $sql['where'] = $where;
        }
        $sql['order'] = $orderby ? $orderby : 'a.viewseq desc,a.createdate desc,a.id desc';
        if($group){
            $sql['group'] = $group;
        }
        if($limit){
            $sql['limit'] = $limit;
        }
        $list = Model::conn('mysql')->select(self::sqlBind($sql));
        if(empty($list)){
            return array();
        }
        foreach($list as $k=>$v){
            $list[$k]['url'] = getEssayUrl($v['id'], $v['tmplinfo'], $v['mould_code']);
        }
        return $list;
    }

    //////////////////////////////////////////【分页】/////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////

    /**
     * Notes:获取分页公共数据-支持异步无限加载
     * User: ZhuHaili
     */
    static function pageListsAjax($table, $menuId=0, $page=1, $limit=0, $where=array(), $orderby='', $field = '', $group = ''){
        $result = array('code'=>'40000', 'msg'=>'fail', 'data'=>array());
        $table = is_string($table) ? $table : '';
        $menuId = is_numeric($menuId) ? $menuId : 0;
        $limit = is_numeric($limit) ? $limit : 0;
        if(!$table){ $result['code'] = '40001';return $result; }
        $db = Model::conn('mysql');
        // 栏目和初始分页数
        if($menuId){
            $menuInfo = $db->findData('sysmenu', array('id'=>$menuId));
            if(empty($menuInfo)){
                $result['code'] = '40003';
                return $result;
            }
            $where['a.menu_id'] = isset($where['a.menu_id']) ? $where['a.menu_id'] : $menuId;
        }
        if(isset($where['a.status'])){
            if($where['a.status']){
                $where['a.status'] = $where['a.status'];
            }
        }else{
            $where['a.status'] = 1;
        }
        $limit = $limit ? $limit : (intval($menuInfo['pagelist']) ? intval($menuInfo['pagelist']) : 10);
        // 分页计算
        $sql['table'] = $table;
        $sql['field'] = 'count(1) counts';
        $sql['where'] = $where;
        $countInfo = $db->conn('mysql')->find(self::sqlBind($sql));
        $counts = isset($countInfo['counts']) ? $countInfo['counts'] : 0;
        $pageSum = $counts ? ceil($counts/$limit) : 1;    // 总页数
        $pageSum = $pageSum > 1 ? $pageSum : 1;
        $pageNo = $page > $pageSum ? $pageSum : $page; // 最大页数==总页数
        $start = $pageNo * $limit - $limit;
        $pages['counts'] = $counts;
        $pages['pageNo'] = $pageNo;
        $pages['pageSum'] = $pageSum;

        // 获取列表数据
        $sql['where'] = $where;
        $sql['field'] = 'a.*,a.id eid,b.*,m.menuname,m.tmpl,m.mould_code';
        $sql['field'] .= $field ? ','.$field : '';
        $sql['table'] = $table;
        $sql['as'] = 'a';
        $sql['leftjoin'][$table.'_detail'] = ' b on a.id=b.essay_id';
        $sql['leftjoin']['sysmenu'] = ' m on a.menu_id=m.id';
        if($GLOBALS['ADMIN']['classmore_flag'] == 1){
            $sql['leftjoin'][$table.'_class'] = ' c on a.id=c.essay_id';
        }
        if($group){
            $sql['group'] = $group;
        }
        $sql['order'] = $orderby ? $orderby : 'a.viewseq desc,a.createdate desc,a.id desc';
        $sql['limit']['start'] = $start;
        $sql['limit']['end'] = $limit;

        $list = $db->select(self::sqlBind($sql));
        foreach($list as $k=>$v){
            $list[$k]['url'] = getEssayUrl($v['id'],$v['tmplinfo'], $v['mould_code']);
        }
        $data['list'] = $list;
        $data['pages'] = $pages;
        $result = array('code'=>'10000', 'msg'=>'success', 'data'=>$data);
        return $result;
    }

    /**
     * Notes: 获取分页数据-动态分页通用-主表
     * User: ZHL
     * Date: 2020/9/30
     */
    static function pageList($table, $menuId=0, $page=1, $limit=0, $where=array(), $orderby='', $field = '', $group = ''){
        $table = is_string($table) ? $table : '';
        $menuId = is_numeric($menuId) ? $menuId : 0;
        $limit = is_numeric($limit) ? $limit : 0;
        $page = is_numeric($page) ? $page : 1;
        $page = intval($page) > 0 ? $page : 1;

        $result = array('code'=>40000, 'msg'=>'数据获取失败', 'data'=>array(), 'pagesArr'=>array(), 'pages'=>'');
        if(!$table){ $result['code'] = 40001;$result['msg'] = 'tableIsNull';return $result; }

        $db = Model::conn('mysql');
        // 菜单分页
        $pagelist = 0;
        if($menuId){
            $menuInfo = $db->findData('sysmenu', array('id'=>$menuId));
            if(!empty($menuInfo)){
                $pagelist = $menuInfo['pagelist']; // 获取后台设置的分页数
            }
            $where['a.menu_id'] = isset($where['a.menu_id']) ? $where['a.menu_id'] : $menuId;
        }
        if(isset($where['a.status'])){
            if($where['a.status']){
                $where['a.status'] = $where['a.status'];
            }
        }else{
            $where['a.status'] = 1;
        }

        $limit = $limit ? $limit : ($pagelist ? $pagelist : 10);

        // 分页总数
        if($where){
            $sql1['where'] = $where;
        }
        $sql1['field'] = 'count(1) counts';
        $sql1['table'] = $table;
        $sql1['as'] = 'a';
        // 多栏目关联
        if($GLOBALS['ADMIN']['classmore_flag'] == 1){
            $sql1['leftjoin'][$table.'_class'] = ' c on a.id=c.essay_id';
        }
        $res = $db->find(self::sqlBind($sql1));
        $dataCount = isset($res['counts']) ? $res['counts'] : 0;

        // 加载分页扩展
        $pageE = new ManagePages($page, $dataCount, $limit);
        $limitArr = $pageE->limitArr;

        // 查询数据
        $sql['field'] = 'a.*,a.id eid,m.tmpl,m.mould_code,m.tmplinfo';
        $sql['field'] .= $field ? ','.$field : '';
        $sql['table'] = $table;
        $sql['as'] = 'a';
        $sql['leftjoin']['sysmenu'] = ' m on a.menu_id=m.id';
        // 多菜单时
        if($GLOBALS['ADMIN']['classmore_flag'] == 1){
            $sql['leftjoin']['essay_class'] = ' c on a.id=c.essay_id';
        }
        if($where){
            $sql['where'] = $where;
        }
        $sql['order'] = $orderby ? $orderby : 'a.viewseq desc,a.createdate desc,a.id desc';
        if($group){
            $sql['group'] = $group;
        }
        if($limit){
            $sql['limit'] = $limitArr;
        }
        $list = $db->select(self::sqlBind($sql));

        foreach($list as $k=>$v){
            $list[$k]['url'] = getEssayUrl($v['id'],$v['tmplinfo'], $v['mould_code']);
        }
        $result['code'] = 10000;
        $result['msg'] = '获取数据成功';
        $result['data'] = $list;
        $result['pagesArr'] = $pageE->show();
        foreach($result['pagesArr'] as $v){
            $result['pages'] .= $v;
        }
        //--------------------
        $list[$k]['pages'] = '';
        foreach($result['pagesArr'] as $v) {
            $list[$k]['pages'] .= $v;
            $list[$k]['pagesArr'] = $result['pagesArr'];
        }
        return $list;
    }

    /**
     * Notes: 获取分页数据-动态分页通用-主表
     * User: ZHL
     * Date: 2020/9/30
     */
    static function pageLists($table, $menuId=0, $page=1, $limit=0, $where=array(), $orderby='', $field = '', $group = ''){
        $table = is_string($table) ? $table : '';
        $menuId = is_numeric($menuId) ? $menuId : 0;
        $limit = is_numeric($limit) ? $limit : 0;
        $page = is_numeric($page) ? $page : 1;
        $page = intval($page) > 0 ? $page : 1;

        $result = array('code'=>40000, 'msg'=>'数据获取失败', 'data'=>array(), 'pagesArr'=>array(), 'pages'=>'');
        if(!$table){ $result['code'] = 40001;$result['msg'] = 'tableIsNull';return $result; }

        $db = Model::conn('mysql');
        // 菜单分页
        $pagelist = 0;
        if($menuId){
            $menuInfo = $db->findData('sysmenu', array('id'=>$menuId));
            if(!empty($menuInfo)){
                $pagelist = $menuInfo['pagelist']; // 获取后台设置的分页数
            }
            $where['a.menu_id'] = isset($where['a.menu_id']) ? $where['a.menu_id'] : $menuId;
        }
        if(isset($where['a.status'])){
            if($where['a.status']){
                $where['a.status'] = $where['a.status'];
            }
        }else{
            $where['a.status'] = 1;
        }

        $limit = $limit ? $limit : ($pagelist ? $pagelist : 10);

        // 分页总数
        if($where){
            $sql1['where'] = $where;
        }
        $sql1['field'] = 'count(1) counts';
        $sql1['table'] = $table;
        $sql1['as'] = 'a';
        // 多栏目关联
        if($GLOBALS['ADMIN']['classmore_flag'] == 1){
            $sql1['leftjoin'][$table.'_class'] = ' c on a.id=c.essay_id';
        }
        $res = $db->find(self::sqlBind($sql1));
        $dataCount = isset($res['counts']) ? $res['counts'] : 0;

        // 加载分页扩展
        $pageE = new ManagePages($page, $dataCount, $limit);
        $limitArr = $pageE->limitArr;

        // 查询数据
        $sql['field'] = 'a.*,a.id eid,b.*,m.tmpl,m.mould_code,m.tmplinfo';
        $sql['field'] .= $field ? ','.$field : '';
        $sql['table'] = $table;
        $sql['as'] = 'a';
        $sql['leftjoin'][$table.'_detail'] = ' b on a.id=b.essay_id';
        $sql['leftjoin']['sysmenu'] = ' m on a.menu_id=m.id';
        // 多菜单时
        if($GLOBALS['ADMIN']['classmore_flag'] == 1){
            $sql['leftjoin']['essay_class'] = ' c on a.id=c.essay_id';
        }
        if($where){
            $sql['where'] = $where;
        }
        $sql['order'] = $orderby ? $orderby : 'a.viewseq desc,a.createdate desc,a.id desc';
        if($group){
            $sql['group'] = $group;
        }
        if($limit){
            $sql['limit'] = $limitArr;
        }
        $list = $db->select(self::sqlBind($sql));

        foreach($list as $k=>$v){
            $list[$k]['url'] = getEssayUrl($v['id'],$v['tmplinfo'], $v['mould_code']);
        }

        $result['code'] = 10000;
        $result['msg'] = '获取数据成功';
        $result['data'] = $list;
        $result['pagesArr'] = $pageE->show();
        foreach($result['pagesArr'] as $v){
            $result['pages'] .= $v;
        }
        //--------------------
        $list[$k]['pages'] = '';
        foreach($result['pagesArr'] as $v) {
            $list[$k]['pages'] .= $v;
            $list[$k]['pagesArr'] = $result['pagesArr'];
        }
        return $list;
    }

    ///////////////////////////////////////【操作】////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////

    /**
     * Notes: 是否允许提交（次数控制）
     * User: ZhuHaili
     * Date: 2022/9/2
     * Desc: $maxCount = 0不限制
     */
    static function subCountFlag($maxCount = 0){
        if(!$maxCount){
            true;
        }
        $messageFlag = SYS_SESSIONS_GET('messageFlag');
        if(!$messageFlag){
            $counts = base64_encode(base64_encode(1));
            SYS_SESSIONS_SET('messageFlag', $counts);
        }else{
            $counts = base64_decode(base64_decode($messageFlag));
            if($counts >= $maxCount){
                $hint['code'] = '40002';
                $hint['msg'] = '你已经提交过多次了，明天再来！';
                return false;
            }
            $counts = base64_encode(base64_encode($counts+1));
            SYS_SESSIONS_SET('messageFlag', $counts);
        }
        return true;
    }

    /**
     * Notes:提交数据有效字段匹配
     * User: ZHL
     * Date: 2020/9/28
     */
    static function planFieldData($fields, $param){
        $data = array();
        foreach($fields as $k=>$v){
            $field = $v['fieldcode'];
            // 字段存在数组中时
            if(array_key_exists($field, $param)){
                if(is_array($param[$field])){
                    $fieldArr = array();
                    foreach($param[$field] as $key=>$val){
                        $fieldArr[$key] = $val;
                    }
                    $fieldStr = implode('|',$fieldArr);
                    $data[$field] = $fieldStr;
                }else {
                    $data[$field] = $param[$field];
                    if($v['fieldtype'] == 5 && $param[$field]){ // 密码
                        $data[$field] = setPwd($param[$field]);
                    }
                }
            }
        }
        return $data;
    }

    /**
     * Notes:提交留言
     * User: ZHL
     * Date: 2022/7/28
     */
    static function addMssage($param, $table, $menuId, $dayCount=0){
        // 验证参数
        if(!$param) return array('code'=>'40000','msg'=>'参数不能为空');
        if(!$table) return array('code'=>'40001','msg'=>'非法请求');
        if(!$menuId) return array('code'=>'40002','msg'=>'非法请求');
        $subFlag = self::subCountFlag($dayCount); // 安全防护，禁止重复多次提交
        if(!$subFlag) return array('code'=>'40003','msg'=>'提交失败，稍后再试！');
        // 匹配字段
        $fieldSer = new Admin_FieldServe();
        $fields1 = $fieldSer->getFieldList($table,'2','1');
        $fields2 = $fieldSer->getFieldList($table,'2','2');
        $data = self::planFieldData($fields1, $param);
        $dataDetail = self::planFieldData($fields2, $param);
        // 插入主表数据
        $data['title'] = isset($param['title']) ? $param['title'] : date('Y-m-d H:i:s').'匿名留言';
        $data['status'] = 1;
        $data['viewseq'] = 0;
        $data['menu_id'] = $menuId;
        $data['createdate'] = date('Y-m-d H:i:s');
        $id = Model::conn('mysql')->addData($table, $data,'id');
        if(!$id){
            return array('code'=>'50001','msg'=>'提交失败，稍后再试！');
        };
        // 插入副表数据
        $dataDetail['essay_id'] = $id;
        $result = Model::conn('mysql')->addData($table.'_detail', $dataDetail);
        if(!$result){
            return array('code'=>'50002','msg'=>'提交失败，稍后再试！');
        }
        return array('code'=>'10000','msg'=>'提交成功');
    }

    /**
     * Notes: 文章阅读量统计
     * User: ZhuHaili
     * Date: 2020/11/6
     * @return string
     */
    static function readCount($table, $id, $field='visit'){
        $result = array('code'=>40000,'msg'=>'Essay count param error', 'readsum'=>0);
        $id = is_numeric($id) ? intval($id) : 0;
        $table = setStr($table);
        if(!$table){return $result;}
        if(!$id){return $result;}
        $db = Model::conn('mysql');
        $where['id'] = $id;
        $info = $db->find($table, $where);
        $readsum = isset($info[$field]) ? intval($info[$field])+1 : 1;
        $table = $db->db_fix.$table;
        $sql = "update $table set $field = $field + 1 where id = $id";
        $res = $db->query($sql);
        if(!$res){
            $result['code'] = 50001;
            $result['msg'] = 'Essay count fail';
            return $result;
        }
        $rowCount = $db->rowCount();
        if(!$rowCount){
            $result['code'] = 50002;
            $result['msg'] = 'Essay count fail';
            return $result;
        }
        $result['code'] = 10000;
        $result['msg'] = 'success';
        $result['readsum'] = $readsum;
        return $result;
    }

    /**
     * Notes: 文章踩赞统计更新
     * User: ZhuHaili
     * Date: 2020/11/6
     * @return string
     */
    static function essayOpsCount($mould, $id, $type){
        $id = is_numeric($id) ? intval($id) : 0;
        $type = is_numeric($type) ? intval($type) : 0;
        $mould = setStr($mould);
        if(($type != 1 && $type != 2) || !$id || !$mould){
            return 'Essay count param error';
        }
        $cookieName = 'essayopsflag_'.$id.'_'.$type;
        $essayopsflag = getCookies($cookieName);
        if($essayopsflag == 'Y'){
            return $type == 1 ? '您已经点赞过了，谢谢你的支持' : '您已经点踩过了，我会更加努力';
        }
        $db = Model::conn('mysql');
        $table = $db->db_fix.$mould;
        if($type == 1){
            $sql = "update $table set laud=laud+1 where id = $id";

        }else{
            $sql = "update $table set oppose=oppose+1 where id = $id";
        }
        $db->query($sql);
        $result = $db->rowCount();
        if($result){
            setCookies($cookieName, 'Y', 86400); //24小时
            return 'Y';
        }
        return '操作失败，稍后再试';
    }

    /**
     * Notes: 文章赞踩数据获取
     * User: ZhuHaili
     * Date: 2020/4/9
     */
    static function essaySupportCount($mould, $id){
        $result = array('code'=>40000,'data'=>array());
        if(!$mould || !$id){
            return $result;
        }
        $where['id'] = $id;
        $arr = self::findData($mould, $where);
        if(empty($arr)){
            return $result;
        }
        $laud = intval($arr['laud']) > 9999 ? '9999+' : intval($arr['laud']);
        $oppose = intval($arr['oppose']) > 9999 ? '9999+' : intval($arr['oppose']);
        $result['code'] = 10000;
        $result['data']['laud'] = $laud;
        $result['data']['oppose'] = $oppose;
        return $result;
    }
}